import { createRouter, createWebHashHistory } from 'vue-router'
import LayOut from '@/layout'
import { isLogin } from '@/utils/user'
const routes = [
  {
    path: '/',
    name: 'LayOut',
    children: [
      {
        path: '/',
        redirect: '/login'
      },
      {
        path: '/dashBoard',
        meta: {
          navs: [
            {
              label: '首页'
            }
          ]
        },
        name: 'dashBoard',
        component: () => import('@/views/DashBoard/index.vue')
      },
      {
        path: '/UserInfo',
        name: 'UserInfo',
        children: [
          {
            path: '/profile',
            name: 'profile',
            component: () => import('@/views/UserInfo/profile.vue')
          },
          {
            path: '/userbank',
            name: 'userbank',
            component: () => import('@/views/UserInfo/userbank.vue')
          },
          {
            path: '/editor',
            name: 'editor',
            component: () => import('@/views/UserInfo/editor.vue')
          }
        ]
      },

      {
        path: '/WithDrawal',
        name: 'WithDrawal',
        children: [
          {
            path: '/applywithdrawl',
            name: 'applywithdrawl',
            component: () => import('@/views/WithDrawal/applywithdrawl.vue')
          },
          {
            path: '/outdrawl',
            name: 'outdrawl',
            component: () => import('@/views/WithDrawal/outdrawl.vue')
          },
          {
            path: '/accountIn',
            name: 'accountIn',
            component: () => import('@/views/WithDrawal/accountIn.vue')
          },
          {
            path: '/depositRecord',
            name: 'depositRecord',
            component: () => import('@/views/WithDrawal/depositRecord.vue')
          },
          {

           /*  path: '/usertransfer',
            name: 'usertransfer',
            component: () => import('@/views/WithDrawal/usertransfer.vue') */
            path: '/usertransferrecordindex',
            name: 'usertransferrecordindex',
            component: () => import('@/views/WithDrawal/usertransferrecordindex.vue')

          },
        ]
      },
      {
        path: '/trades',
        name: 'trades',
        component: () => import('@/views/Trades')
      },
      {
        path: '/CustomerMassmessage',
        name: 'CustomerMassmessage',
        component: () => import('@/views/Massmessage')
      },
      {
        path: '/LeaveComments',
        name: 'LeaveComments',
        component: () => import('@/views/LeaveComments/index.vue')
      },
    ],
    component: LayOut
  },
  {
    path: '/login',
    name: 'login',
    meta: {
      roles: '*'
    },
    component: () => import('@/views/Login/index.vue')
  },
  {
    path: '/register',
    name: 'register',
    meta: {
      roles: '*'
    },
    component: () => import('@/views/Register')
  },
  {
    path: '/retrieve',
    name: 'retrieve',
    meta: {
      roles: '*'
    },
    component: () => import('@/views/Retrieve')
  },
  {
    path: '/:pathMatch(.*)*',
    component: () => import('@/views/NotFound.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

const publicRoutes = ['/login', '/register', '/retrieve'];

router.beforeEach((to, from, next) => {
  if (publicRoutes.includes(to.path)) {
    // 如果访问的是公共页面，如注册和找回密码页面，直接放行
    next();
  } else {
    // 验证 token 是否存在
    if (isLogin()) {
      // 如果存在 token，允许访问其他页面
      next();
    } else {
      // 如果不存在 token，跳转到登录页面
      next('/login');
    }
  }
});

export default router
